package backend.project.gym_management.controller;

import backend.project.gym_management.pojo.entity.CourseImg;
import backend.project.gym_management.pojo.entity.EAvatar;
import backend.project.gym_management.service.UploadFileService;
import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.base.response.StatusCode;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@Api(tags = "06.上传文件模块")
@RestController
@RequestMapping("/v1/upload")
public class UploadController {
    @Autowired
    private UploadFileService uploadFileService;
    public static final String filePath = System.getProperty("user.dir")+"/gym_front/public/static/images/";
    @PostMapping("/e_avatar")
    public JsonResult uploadEAvatar(MultipartFile file,Long eid) throws Exception {
        log.debug("eid{}",eid);
        //1.获取用户上传的原始文件名
        String fileName = file.getOriginalFilename();
        //2.截取文件后缀名
        String suffix = null;
        log.debug("文件名:{}",fileName);
        if(fileName!=null){
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }else return new JsonResult(StatusCode.OPERATION_FAILED,"上传失败");
        //3.起一个不重复的新文件名
        fileName = UUID.randomUUID()+suffix;
        //4.指定一个路径用来存储上传的图片文件,注意:这个路径必须自己电脑上有!
        String dirPath = filePath+"avatar/e_avatar/";
        String filePath = dirPath+fileName;
        file.transferTo(new File(filePath));
        log.debug("图片上传成功,图片路径:{}",filePath);
        var data = "http://localhost:9090/static/images/avatar/e_avatar/"+fileName;
        var avatar = uploadFileService.selectEAvatar(eid);
        if (avatar!=null){
            uploadFileService.deleteEAvatar(eid);
            var avatarPath = avatar.getAvatarPath();
            var deleFileName = avatarPath.substring(avatarPath.lastIndexOf("/")+1);
            var deleFilePath = dirPath+deleFileName;
            var deleFile = new File(deleFilePath);
            log.debug("deleFilePath{}",deleFilePath);
            if (deleFile.delete()) {
                log.debug("头像已被删除");
            } else {
                log.debug("删除头像失败");
            }
        }
        uploadFileService.insertEAvatar(new EAvatar(eid,data));
        return JsonResult.ok(data);
    }
    @PostMapping("/courseImg")
    public JsonResult uploadCourseImg(MultipartFile file,Long cid) throws Exception {
        log.debug("cid{}",cid);
        //1.获取用户上传的原始文件名
        String fileName = file.getOriginalFilename();
        //2.截取文件后缀名
        String suffix = null;
        log.debug("文件名:{}",fileName);
        if(fileName!=null){
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }else return new JsonResult(StatusCode.OPERATION_FAILED,"上传失败");
        //3.起一个不重复的新文件名
        fileName = UUID.randomUUID()+suffix;
        //4.指定一个路径用来存储上传的图片文件,注意:这个路径必须自己电脑上有!
        String dirPath = filePath+"course_img/";
        String filePath = dirPath+fileName;
        file.transferTo(new File(filePath));
        log.debug("图片上传成功,图片路径:{}",filePath);
        var data = "http://localhost:9090/static/images/course_img/"+fileName;
        var courseImg = uploadFileService.selectCourseImg(cid);
        if(courseImg!=null){
            uploadFileService.deleteCourseImg(cid);
            var courseImgPath = courseImg.getImgPath();
            var deleFileName = courseImgPath.substring(courseImgPath.lastIndexOf("/")+1);
            var deleFilePath = dirPath+deleFileName;
            var deleFile = new File(deleFilePath);
            log.debug("deleFilePath{}",deleFilePath);
            if(deleFile.delete()){
                log.debug("课程图片已被删除");
            }else {
                log.debug("删除课程图片失败");
            }
        }
        uploadFileService.insertCourseImg(new CourseImg(cid,data));
        return JsonResult.ok(data);
    }
}
